package com.example.wanandroid.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import com.example.wanandroid.R;
import com.example.wanandroid.base.BaseActivity;
import com.example.wanandroid.fragment.SystemDataFragment;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class SystemTablayoutActivity extends BaseActivity {

    //TabLayout
    private TabLayout mTabLayout;
    //ViewPager
    private ViewPager mViewPager;
    //Title
    private List<String> mTitle=new ArrayList<>();
    //Fragment
    private List<Fragment> mFragment=new ArrayList<>();
    private String tabName;
    private String tabId;
    private SystemDataFragment systemDataFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_system_tablayout);

        Intent intent=getIntent();
        String name=intent.getStringExtra("name");
        tabName=intent.getStringExtra("mPname");
        tabId=intent.getStringExtra("mPid");

        //设置标题
        getSupportActionBar().setTitle(name);
        initView();
        initData();
    }
    private void initData() {
        ArrayList list=new ArrayList(Arrays.asList(tabName.split(",")));
        ArrayList listId=new ArrayList(Arrays.asList(tabId.split(",")));

        for (int j=0;j<listId.size();j++){
            systemDataFragment=new SystemDataFragment();
            Bundle bundle = new Bundle();
            bundle.putString("mId", String.valueOf(listId.get(j)));
            systemDataFragment.setArguments(bundle);
            mFragment.add(systemDataFragment);
            mTitle.add(String.valueOf(list.get(j)));
            initFragment(mFragment,mTitle);
        }
    }

    private void initView() {
        mTabLayout = findViewById(R.id.mTabLayout);
        mViewPager = findViewById(R.id.mViewPager);
    }

    private void initFragment(final List<Fragment> mFragment, final List<String> mTitle) {
        //设置设配器
        mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            //选中的item
            @Override
            public Fragment getItem(int position) {
                return mFragment.get(position);
            }
            //返回item的个数
            @Override
            public int getCount() {
                return mFragment.size();
            }
            //设置标题
            @Override
            public CharSequence getPageTitle(int position) {
                return mTitle.get(position);
            }
        });
        //绑定
        mTabLayout.setupWithViewPager(mViewPager);
    }
}
